\documentclass[11pt,twoside,openright]{book}

% Essential packages for XeLaTeX
% Note: inputenc and fontenc are not needed with XeLaTeX
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}
\setmonofont{Latin Modern Mono}
\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{titlesec}

% Mathematical packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{bm}
\usepackage{bbm}

% Algorithm packages
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{listings}

% Define Function command for algorithm2e
\SetKwProg{Fn}{Function}{:}{end}

% Graphics and figures
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{graphicx}
\usepackage{subcaption}

% Tables and formatting
\usepackage{booktabs}
\usepackage{array}
\usepackage{multirow}
\usepackage{longtable}

% References and links
\usepackage[backend=biber,style=alphabetic,sorting=ynt]{biblatex}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}
\usepackage{makeidx}

% Color and styling
\usepackage{xcolor}
\usepackage{mdframed}

% Counter management
\usepackage{chngcntr}

% Appendix package for better appendix formatting
\usepackage[titletoc,title]{appendix}

% Bibliography file
\addbibresource{bibliography.bib}

% Make index
\makeindex

% Page geometry
\geometry{
    a4paper,
    left=3cm,
    right=2.5cm,
    top=2.5cm,
    bottom=2.5cm,
    headheight=14pt
}

% Custom colors
\definecolor{theoremcolor}{RGB}{230,240,255}
\definecolor{definitioncolor}{RGB}{240,255,240}
\definecolor{lemmacolor}{RGB}{255,245,230}
\definecolor{algorithmcolor}{RGB}{245,245,245}

% Theorem environments
\newtheoremstyle{booktheorem}
{10pt}{10pt}{\itshape}{}{\bfseries}{.}{ }{}

\theoremstyle{booktheorem}
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{remark}[theorem]{Remark}
\newtheorem{assumption}[theorem]{Assumption}

% Custom theorem boxes
\mdfdefinestyle{theoremstyle}{
    backgroundcolor=theoremcolor,
    linecolor=blue!60,
    linewidth=1pt,
    roundcorner=5pt,
    innertopmargin=10pt,
    innerbottommargin=10pt,
    innerleftmargin=10pt,
    innerrightmargin=10pt
}

\mdfdefinestyle{definitionstyle}{
    backgroundcolor=definitioncolor,
    linecolor=green!60,
    linewidth=1pt,
    roundcorner=5pt,
    innertopmargin=10pt,
    innerbottommargin=10pt,
    innerleftmargin=10pt,
    innerrightmargin=10pt
}

% Custom commands
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\Prob}{\mathbb{P}}
\newcommand{\Var}{\text{Var}}
\newcommand{\Cov}{\text{Cov}}

% Claude Code specific notation
\newcommand{\ClaudeCode}{\textsc{Claude Code}}
\newcommand{\Agent}{\mathcal{A}}
\newcommand{\Tools}{\mathcal{T}}
\newcommand{\Context}{\mathcal{C}}
\newcommand{\StateSpace}{\mathcal{S}}
\newcommand{\Actions}{\mathcal{U}}
\newcommand{\Observation}{\mathcal{O}}
\newcommand{\Belief}{\mathcal{B}}

% Header and footer
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\rightmark}
\fancyhead[RE]{\leftmark}

% Title page information
\title{
    \Huge\textbf{Theoretical Foundations of Claude Code} \\
    \vspace{0.5cm}
    \Large Mathematical Models and Algorithmic Analysis \\
    \Large of AI-Powered Code Intelligence Systems
}

\author{
    \Large Based on the theoretical framework developed \\
    \Large from Claude-Code-Theory.txt and Claude-Code-Theory-Review.txt
}

\date{\Large \today}

\begin{document}

% Title page
\frontmatter
\maketitle

% Copyright page
\newpage
\thispagestyle{empty}
\vspace*{\fill}
\begin{center}
\textcopyright{} 2025 \\
\vspace{0.5cm}
This work presents a comprehensive mathematical theory for AI-powered code intelligence systems, with specific focus on \ClaudeCode{} architecture and mechanisms.
\end{center}
\vspace*{\fill}

% Table of contents
\tableofcontents

% List of figures and tables
\listoffigures
\listoftables

% Preface
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}

This book presents a comprehensive mathematical and algorithmic foundation for understanding AI-powered code intelligence systems, with particular focus on \ClaudeCode{}. The theoretical framework developed here addresses the fundamental challenges of building intelligent systems that can understand, generate, and manipulate code through tool-assisted interactions.

The work builds upon extensive analysis of \ClaudeCode{}'s operational patterns, system architecture, and behavioral characteristics. We formalize the system as a hierarchical Partially Observable Markov Decision Process (POMDP) enhanced with options framework, contextual bandit algorithms, and multi-objective optimization techniques.

Our theoretical contributions include:
\begin{itemize}
    \item Rigorous mathematical models for agent-tool interaction patterns
    \item Algorithmic analysis with provable performance guarantees  
    \item Information-theoretic foundations for context management
    \item Queueing-theoretic treatment of concurrent operations
    \item Safety and verification frameworks with formal guarantees
    \item Empirical validation methodologies with statistical rigor
\end{itemize}

The book is structured to serve both theoretical researchers seeking to understand the mathematical foundations and practitioners implementing similar systems. Each chapter builds upon previous results while remaining as self-contained as possible.

\vspace{1cm}
\noindent\textit{The Authors} \\
\today

% Main content
\mainmatter

% Include chapters
\include{chapters/chapter0_overview}
\include{chapters/chapter1_introduction}
\include{chapters/chapter2_formal_model}
\include{chapters/chapter3_mathematical_models}
\include{chapters/chapter4_algorithmic_analysis}
\include{chapters/chapter5_performance_optimization}
\include{chapters/chapter6_safety_verification}
\include{chapters/chapter7_empirical_validation}

% Back matter
\backmatter

% Use appendices environment instead of \appendix command  
\begin{appendices}
\include{appendices/appendix_a_proofs}
\include{appendices/appendix_b_algorithms}
\include{appendices/appendix_c_mathematical_foundations}  
\include{appendices/appendix_d_statistical_learning}
\include{appendices/appendix_e_optimization}
\end{appendices}

% Bibliography
\printbibliography[heading=bibintoc]

% Index
\printindex

\end{document}